using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.Json;
using System.Threading;
using Bee.Models.Menu;
using Bee.ViewModels;
using Ke.Bee.Localization.Extensions;
using Ke.Bee.Localization.Options;
using Ke.Bee.Localization.Providers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Bee;

public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 服务注册
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddServices(this IServiceCollection services)
    {
        services.AddTransient<MainWindowViewModel>();
        
        // 从配置文件中读取菜单注入到 DI 容器
        var menuItemString = File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "Configs", "menus.json"));
        var menuItems = JsonSerializer.Deserialize<MenuItem[]>(menuItemString);
        services.AddSingleton(Options.Create(menuItems));
        
        // 注册本地化
        services.AddLocalization();
        
        return services;
    }

    private static IServiceCollection AddLocalization(this IServiceCollection services)
    {
        services.AddLocalization<AvaloniaJsonLocalizationProvider>(() =>
        {
            var options = new AvaloniaLocalizationOptions(
                // 支持本地化语言文化
                new List<CultureInfo>
                {
                    new("en-US"),
                    new("zh-CN")
                },
                // 默认选项，当设置当前文化（currentCulture）不再 cultures 列表中时，作为缺失的本地化条目 
                new CultureInfo("en-US"),
                // currentCulture 在基础设施加载时设置，可以从应用程序设置或其他地方获取
                Thread.CurrentThread.CurrentCulture,
                // 包含本地化 json 文件的资源路径
                $"{typeof(App).Namespace}/Assets/i18n"
            );
            return options;
        });
        return services;
    }
}